home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / basics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-14  |  2.6 KB  |  120 lines

  1.  
  2. /* Copyright (C) 1988, 1989 Herve' Touati, Aquarius Project, UC Berkeley */
  3.  
  4. /* Copyright Herve' Touati, Aquarius Project, UC Berkeley */
  5.  
  6. #include <stream.h>
  7. #include "tags.h"
  8. #include "instr.h"
  9. #include "hash_table.h"
  10. #include "string_table.h"
  11. #include "memory.h"
  12. #include "basics.h"
  13. #include "top_level.h"
  14.  
  15. unsigned MODE;
  16.  
  17.  /* conventions: */
  18.  /* a DATA POINTER is stored as an OFFSET from H0 */
  19.  /* a STACK POINTER register is stored by casting it to UNSIGNED */
  20.  /* a CODE POINTER is stored by casting it to UNSIGNED */
  21.  
  22. Cell Deref(register Cell Ref) 
  23. {
  24.   while (get_tag(Ref) == TAGREF) {
  25.     register Cell Val = lvalue(Ref);
  26.     if (Ref == Val) break;
  27.     Ref = Val;
  28.   }
  29.   return Ref;
  30. }
  31.  
  32. unsigned unify(register Cell arg1, register Cell arg2) 
  33. {
  34.  top_of_the_loop:
  35.   arg1 = deref(arg1);
  36.   arg2 = deref(arg2);
  37.   if (get_tag(arg1) == get_tag(arg2)) {
  38.     switch(get_tag(arg1)) {
  39.     case TAGREF:
  40.       if (arg1 > arg2)
  41.     Bind(arg1, arg2);
  42.       else
  43.     Bind(arg2, arg1);
  44.       return UNIFY_SUCCESS;
  45.     case TAGLIST:
  46.       {
  47.     if (arg1 == arg2) return UNIFY_SUCCESS;
  48.     CellPtr S1 = addr(arg1);
  49.     CellPtr S2 = addr(arg2);
  50.     if (! unify(S1[0], S2[0])) return UNIFY_FAIL;
  51.     arg1 = S1[1];
  52.     arg2 = S2[1];
  53.     goto top_of_the_loop;
  54.       }
  55.     case TAGSTRUCT:
  56.       {
  57.     if (arg1 == arg2) return UNIFY_SUCCESS;
  58.     CellPtr S1 = addr(arg1);
  59.     CellPtr S2 = addr(arg2);
  60.     if (S1[0] != S2[0]) return UNIFY_FAIL;
  61.     int i0 = get_int(S1[1]) + 2;
  62.     for (int i = 2; i < i0; i++)
  63.       if (! unify(S1[i], S2[i])) return UNIFY_FAIL;
  64.     break;
  65.       }
  66.     case TAGCONST:
  67.       return (arg1 == arg2) ? UNIFY_SUCCESS : UNIFY_FAIL;
  68.     default:
  69.       top_level_error("strange tag encountered");
  70.       break;
  71.     }
  72.   } else if (get_tag(arg1) == TAGREF) {
  73.     Bind(arg1, arg2);
  74.     return UNIFY_SUCCESS;
  75.   } else if (get_tag(arg2) == TAGREF) {
  76.     Bind(arg2, arg1);
  77.     return UNIFY_SUCCESS;
  78.   } else {
  79.     return UNIFY_FAIL;
  80.   }
  81. }
  82.  
  83.  /* Special halt instruction */
  84. void Halt() 
  85. {
  86.   cout << "\t\t\t*** HALT *** \n";
  87.   top_level_normal_termination();
  88. }
  89.  
  90. void Fail() 
  91. {
  92.  /* undo the trailed bindings */
  93.  /* the trail stack grows downwards */
  94.  /* the trail entries are guaranteed to be unbound variables  */
  95.   register Cell* tr = TR + 1;
  96.   register Cell* tr0 = cellp(B[TR_CP_OFFSET]);
  97.   for (; tr <= tr0; tr++)
  98.     lvalue(*tr) = *tr;
  99.   TR = tr0;
  100.   
  101.  /* read the topmost choice point back into registers  */
  102.   E = cellp(B[E_CP_OFFSET]);
  103.   H = cellp(B[H_CP_OFFSET]);
  104.   TR = cellp(B[TR_CP_OFFSET]);
  105.   P = instrp(B[P_CP_OFFSET]);
  106.   int i0 = B[SIZE_CP_OFFSET];
  107.   for (int i = 0; i < i0; i++)
  108.     X[i] = B[X1_CP_OFFSET + i];
  109.  
  110. #ifdef WITH_GC
  111.   if (H < HMIN) {
  112.     H2 = H;
  113.     TR2 = TR;
  114.     E2 = E;
  115.     H = HMIN;
  116.   }
  117. #endif
  118. }
  119.  
  120.